<?php
namespace app\controllers\api\post;

use fate\http\Request;
use app\models\Posts;
use app\models\Files;
use app\models\UserCredit;
use app\models\UserCreditLog;
use app\libs\LibUser;
use app\libs\Html;
use app\libs\Util;
use app\libs\Sys;
use app\libs\Uploader;
use app\controllers\api\upload\PreController;

class AddController extends \fate\web\PureController {

    public function run() {
        global $_G;

        // 是否登录
        $uid = LibUser::getLoginUid();
        if('' === $uid) {
            $this->ajaxReturn(null, 200001, 'not login');
            exit;
        }

        $req = Request::getInstance();
        $exp = Sys::getPostExp();
        $contentStatus = Sys::isOpenReview() ? 0 : 1;

        // 关闭投稿
        if(!Sys::canPost($uid)) {
            $this->ajaxReturn(null, 500002, 'post closed');
            exit;
        }

        $articleConfig = Sys::getArticleLengthConfig();
        $thumb = $req->getParameter('thumb');
        $title = $req->getParameter('title');
        $content = $req->getParameter('content');
        $tags = $req->getParameter('tags');

        $readCateId = $req->getParameter('cate_id');
        $copyright = $req->getParameter('copyright');
        $brief = empty($req->getParameter('brief')) ? '' : $req->getParameter('brief');

        // check
        if(!is_numeric($readCateId)) {
            $this->ajaxReturn(null, 300002, 'category id error');
            exit;
        }
        if(!is_numeric($copyright)) {
            $this->ajaxReturn(null, 300002, 'copyright error');
            exit;
        }
        if(!$thumb) {
            $this->ajaxReturn(null, 300002, 'thumb image error');
            exit;
        }
        if(!$title) {
            $this->ajaxReturn(null, 300002, 'title error');
            exit;
        }

        $plain = Util::plainText($content);
        if( Util::strlenOfCN($plain) < $articleConfig['min'] || Util::strlenOfCN($plain) > $articleConfig['max'] ) {
            $this->ajaxReturn(null, 300002, 'content too long');
            exit;
        }

        // 保存头图
        $uploader = new Uploader();
        $uploader->setConfigs([
            'basePath' => './upload/thumb'
        ]);
        $uploader->saveBase64Image($thumb);
        $thumb = $uploader->getFirstUploadedFile();

        if('' === $thumb) {
            $this->ajaxReturn(null, 300003, 'thumb upload error');
            exit;
        }

        $thumb = '/upload/thumb' . $thumb;
        $filter = new Html();
        $fileModel = new Files();
        $postModel = new Posts();
        $userCreditModel = new UserCredit();
        $userCreditLogModel = new UserCreditLog();

        $fileModel->db->beginTransaction();
        try {
            $fileModel->insert([
                'uid' => $uid,
                'uri' => $thumb,
                'upload_ip' => $req->getUserIP(),
                'project_id' => PreController::$PROJECT_ID_COMMON_ARTICLE,
                'post_time' => $_G['now_time']
            ]);
            $postModel->insert([
                'uid' => $uid,
                'thumb' => $thumb,
                'title' => $title,
                'content' => $filter->pure($content),
                'tags' => $tags,
                'cate_id' => $readCateId,
                'copyright' => $copyright,
                'brief' => Util::plainText($brief),
                'post_time' => $_G['now_time'],
                'status' => $contentStatus
            ]);

            // 经验
            if($exp > 0) {
                $userCreditModel->addExp($uid, $exp);
                $userCreditLogModel->insert([
                    'uid' => $uid,
                    'experience' => $exp,
                    'action' => UserCreditLog::$ACTION_POST_ARTICLE,
                    'log_time' => $_G['now_time']
                ]);
            }

            $fileModel->db->commitTransaction();
            $this->ajaxReturn(null);

        } catch(\Exception $e) {
            $fileModel->db->rollbackTransaction();
            return;
        }
    }

}
